﻿namespace Mytodotodaylist.WF.Client.Views.AddEditView
{
    using System;
    using System.Windows.Forms;
    using Core.Events;

    public partial class AddEditFormImpl : Form, IAddEditView
    {
        public AddEditFormImpl()
        {
            InitializeComponent();
        }

        private Event Model;

        public void SetModel(Event ev1)
        {
            Model = ev1;
            cbEventType.DataSource = Enum.GetValues(typeof(Periodicity));

            if(ev1 !=  null)
            {
                nupdHour.Value = ev1.Date.Hour;
                nupdMin.Value = ev1.Date.Minute;
                tbName.Text = ev1.Name;
                cbEventType.SelectedItem = ev1.Frequency;
            }
        }

        public Event GetModel()
        {
            if(Model == null)
                Model = new Event();

            var dtpSelDate = dtpDate.Value;

            Model.Date = new DateTime(
                dtpSelDate.Year, 
                dtpSelDate.Month, 
                dtpSelDate.Day,
                (int)nupdHour.Value,
                (int)nupdMin.Value, 
                0);

            Model.Name = tbName.Text;
            Model.Frequency = (Periodicity) Enum.Parse(typeof (Periodicity), cbEventType.SelectedItem.ToString());

            return Model;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}